package com.sap.jcp.sm.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;
import com.sap.jcp.sm.domain.Shift;
import com.sap.jcp.sm.domain.User;
import com.sap.jcp.sm.domain.UserTimeline;
import com.sap.jcp.sm.dto.UserTimelineDTO;

public class UserTimelineDAOImpl implements UserTimelineDAO {

	@SessionTarget
	Session session;
	@TransactionTarget
	Transaction transaction;
	
	
	/*@SuppressWarnings("unchecked")
	public List<Object[]> listUserTimeline(String weekId) {
		List<Object[]> resultSet = null;
		try {
			System.out.println("UserTimeline");
			String sql = "select usr.USER_NAME name, usr.USER_ID user_id, usr.EMAIL_ID email, usr.PRIMARY_TRACK primary_track, " +
					"usr.ORACLE_ID oracle_id, utl.TRK_ID track_id, utl.SHIFT_ID shift_id, utl.week_id week_id, " +
					"utl.day day, utl.month month, utl.year year from user usr LEFT JOIN user_timeline utl " +
					"ON usr.user_id = utl.user_id and utl.week_id =:weekId where usr.status =:status";
			Query query = session.createSQLQuery(sql);
			query.setParameter("weekId", weekId);
			query.setParameter("status", "Approved");
			resultSet = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultSet;
	}*/
	
	@SuppressWarnings("unchecked")
	public List<UserTimeline> listUserTimeline(String weekId) {
		String hql = "from UserTimeline where week = :weekId";
		Query query = session.createQuery(hql);
		query.setParameter("weekId", weekId);
		List<UserTimeline> userTimelineList = query.list();
		return userTimelineList;
	}
	
	public UserTimeline getTimeLine(String userTimelineId) {
		
		String hql = "from UserTimeline where userTimelineId = :userTimelineId";
		Query query = session.createQuery(hql);
		query.setString("userTimelineId", userTimelineId);
		List<UserTimeline> timelineList = query.list();
		if (timelineList != null && !timelineList.isEmpty()) {
			return timelineList.get(0);
		}
		return null;
	}

	public void saveTimeLine(UserTimeline userTimeline) {
		try {
			transaction.begin();
			session.save(userTimeline);
			transaction.commit();
			session.flush();
		    session.clear();
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}
	}
	
	public void updateTimeLine(UserTimeline userTimeline) {
		try {
			transaction.begin();
			session.update(userTimeline);
			transaction.commit();
			session.flush();
		    session.clear();
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}
	}
	@Override
	public void deleteSchedule(UserTimeline userTimeline) {
		transaction.begin();
		session.delete(userTimeline);
		transaction.commit();
	}
	
	@SuppressWarnings("unchecked")
	public List<UserTimeline>  getLastFiveSchedule(List<String> weekIdList) {
		String hql = "from UserTimeline where week in (:weekIdList) order by week";
		Query query = session.createQuery(hql);
		query.setParameterList("weekIdList", weekIdList);
		List<UserTimeline> userTimelineList = query.list();
		return userTimelineList;
	}
	
}
